package com.weipai.demo.util;


import org.apache.commons.lang3.RandomStringUtils;


import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * 获取编码
 * Prefix 自行添加
 * Created by zhanglong on 2017/11/23.
 *
 * @author M6800
 * @version $Id: $Id
 */
public class Code {

   /* *//**
     * 后6位随机，唯一性需自己校验
     *
     *
     * @return a {@link String} object.
     *//*
    public static String randomCode(Prefix prefix){
        String middle = DateTimeFormatter.ofPattern("YYMMdd").format(LocalDate.now());
        String suffix = RandomStringUtils.randomNumeric(6);
        return createCode(prefix, middle, suffix);
    }*/

    /**
     * 后6位随机，唯一性需自己校验
     *
     * @param firstPart a {@link String} object.
     * @return a {@link String} object.
     */
    public static String randomCode(Prefix prefix, String firstPart){
        String middle = DateTimeFormatter.ofPattern("YYMMdd").format(LocalDate.now());
        String suffix = RandomStringUtils.randomNumeric(6);
        return createCode(prefix, firstPart, middle, suffix);
    }
    /**
     * 后6位随机，唯一性需自己校验
     *
     * @return a {@link String} object.
     */
    public static String randomCodeQD(Prefix prefix, FirstPart firstPart){
        String middle = DateTimeFormatter.ofPattern("YYMMdd").format(LocalDate.now());
        String suffix = RandomStringUtils.randomNumeric(6);
        return createCode(prefix, firstPart.getValue(), middle, suffix);
    }
   /* *//**
     * 后6位自增
     *
     * @return a {@link String} object.
     *//*
    public static String createCode(Prefix prefix){
        return createCode(prefix, "");
    }*/

   /* *//**
     * <p>createCode.</p>
     *
     * @param firstPart a {@link String} object.
     * @return a {@link String} object.
     *//*
    public static String createCode(Prefix prefix, String firstPart){
        String middle = DateTimeFormatter.ofPattern("YYMMdd").format(LocalDate.now());
        return createCode(prefix, firstPart, middle);
    }
*/


    /**
     * 编码由四部分组成
     *
     * @param prefix 业务前缀
     * @param firstPart 空字符串或渠道号或其他
     * @param secondPart 一般是6位时间戳
     * @param suffix 一般自增 或随机
     * @return a {@link String} object.
     */
    public static String createCode(Prefix prefix, String firstPart, String secondPart, String suffix){
        return prefix.getValue() + firstPart + secondPart + suffix;
    }


    public enum Prefix {
        demo_CODEE("人员单号", "DON");//swap out archiveNo

        private String label;
        private String value;

        Prefix(String label, String value) {
            this.label = label;
            this.value = value;
        }

        protected String getLabel() {
            return label;
        }

        protected String getValue() {
            return value;
        }
    }

    public enum FirstPart {
        WANGZHAN("网站", "011");
        private String label;
        private String value;

        FirstPart(String label, String value) {
            this.label = label;
            this.value = value;
        }

        protected String getLabel() {
            return label;
        }

        protected String getValue() {
            return value;
        }
    }

}
